﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Shopping_Cart
/// </summary>
/// 


namespace Bestlife.ERP.BusinessRules
{

    #region Create Cart ItemList

    public class Shopping_Cart
    {
    
    List<Entities.Cart_Item> items;
    public List<Entities.Cart_Item> Items
    {
        get { return items; }
        set { items = value; }
    }

    #endregion

    #region Shopping CartList

    public Shopping_Cart()
	{
        items = new List<Entities.Cart_Item>();
	}

    #endregion

    #region Find Correct Index

    public int Findindex(string Itemid, string code)
    {
        int index = -1;
        foreach (Entities.Cart_Item c in items)
        {
            index++;
            if (c.Produt_ID.Equals(Itemid) && c.Product_Code.Equals(code))
            {

                return index;
            }

        }

        return -1;

    }

    #endregion 

    #region Insert Shopping Cart Item

    public void insertItems(string id, string code, string name, decimal pri, decimal pointv, decimal busi_val, int qun)
    {
        int index = Findindex(id, code);

        if (index != -1)
        {
            items[index].Quantity += qun;

        }
        else
        {
            Entities.Cart_Item c = new Entities.Cart_Item();
            c.Produt_ID = id;
            c.Product_Code = code;
            c.Product_Name = name;
            c.Product_Price = double.Parse(pri.ToString());
            c.Point_Value = float.Parse(pointv.ToString());
            c.Business_value = float.Parse(busi_val.ToString());
            c.Quantity = qun;
            items.Add(c);

        }


    }

    #endregion

    #region Total Calculate

    public double total
    {
        get
        {
            double tot = 0;
            foreach (Entities.Cart_Item c in items)
            {
                tot = tot + c.Tot_Price;
            }
            return tot;
        }


    }

    #endregion

    #region Total Point_value

    public double totalPoint_value
    {
        get
        {
            double totpv = 0;
            foreach (Entities.Cart_Item c in items)
            {
                totpv = totpv + c.Tot_Point_Value;
            }
            return totpv;
        }


    }

    #endregion

    #region Total Business_Value

    public double totalBusiness_Value
    {
        get
        {
            double totbv = 0;
            foreach (Entities.Cart_Item c in items)
            {
                totbv = totbv + c.Tot_Business_Value;
            }
            return totbv;
        }


    }

    #endregion

    #region Update  Cart

    public void updateItems(string id, string code, string name, double pri, float pointv, float busi_val, int qun)
    {
        int index = Findindex(id, code);

        if (index != -1)
        {
            items[index].Quantity = qun;
            Entities.Cart_Item c = new Entities.Cart_Item();
            c.Produt_ID = id;
            c.Product_Code = code;
            c.Product_Name = name;
            c.Product_Price = pri;
            c.Point_Value = pointv;
            c.Business_value = busi_val;
            c.Quantity = qun;
            items.Add(c);
            items.RemoveAt(index);
        }
    }


    #endregion

    }
}
